// pages/select_product/index.js
import {
    getNavigationbar
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        details: {},
        scrollViewHeight: 0,
        goodsList: [{
                goodsImg: "/images/goods-cat.jpeg",
                goodsName: "商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出",
                check: false
            },
            {
                goodsImg: "/images/goods-cat.jpeg",
                goodsName: "商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出",
                check: false
            },
            {
                goodsImg: "/images/goods-cat.jpeg",
                goodsName: "商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出",
                check: false
            },
            {
                goodsImg: "/images/goods-cat.jpeg",
                goodsName: "商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出商品名称文本演示自动换行最高限制2行超出",
                check: false
            },
        ],
        allCheckVal: false,
    },
    handlecose() {
        wx.navigateBack()
    },
    gethandle() {
        getNavigationbar({
            act: 'mall_stock_list',
            orderid: this.data.details.id || '',
            is_gift: 0 || '',

        }).then(res => {
            console.log('一件代发', res)
            res.forEach(element => {
               Object.assign(element,{
                orderid:this.data.details.id
               }) 
            });
            this.setData({
                goodsList:res
            })
        })
    },

    /**
     * 选择商品回调函数方法
     * @param {*} e 
     */
    handleSelCheck(e) {
        const {
            index
        } = e.currentTarget.dataset
        let goodsList = this.data.goodsList // 获取购物车数据
        if (goodsList[index].check) {
            goodsList[index].check = false
        } else {
            goodsList[index].check = true
        }
        // 重新赋值 渲染购物车数据
        this.setData({
            goodsList: goodsList
        })
        this.getAllCheck()
    },
    /**
     * 商品全勾选上设置全选
     */
    getAllCheck() {
        let goodsList = this.data.goodsList // 获取商品列表数据
        let length = goodsList.length // 商品列表数量长度
        let allCheckVal = this.data.allCheckVal // 当前全选状态
        let allCheck = 0
        for (let i = 0; i < goodsList.length; i++) {
            if (goodsList[i].check) {
                allCheck++
            }
        }
        if (length === allCheck) {
            allCheckVal = true
        } else {
            allCheckVal = false
        }
        this.setData({
            allCheckVal: allCheckVal
        })
    },
    /**
     * 全选回调函数方法
     * @param {*} e 
     */
    allCheckSelChange() {
        // 购物车为空时无法勾选，返回重新校验
        if (this.data.goodsList.length == 0) return
        const check = this.data.allCheckVal = !this.data.allCheckVal
        let goodsList = this.data.goodsList // 获取商品列表数据
        for (let i = 0; i < goodsList.length; i++) {
            if (check) {
                goodsList[i].check = check
            } else {
                goodsList[i].check = check
            }
        }
        this.setData({
            allCheckVal: check,
            goodsList: goodsList
        })
    },
    /**
     * 下一步操作回调函数方法
     */
    handleNextStep() {
    
        let selectedGoods = this.data.goodsList.filter(item => item.check); // 过滤出被选中的商品
        if (selectedGoods.length === 0) {
            wx.showToast({
                title: '请先选择商品',
                icon: 'none',
                duration: 2000
            });
            return;
        }
        wx.navigateTo({
            url: '/pages/goods_set_class/index?data=' + JSON.stringify(selectedGoods),
        });
        console.log('this.data.goodsList',selectedGoods)
    },
    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
        let that = this
        let query = wx.createSelectorQuery().in(this)
        query.select('.header').boundingClientRect()
        query.select('.footer').boundingClientRect()
        query.exec(res => {
            let headerHeight = res[0].height;
            let footerHeight = res[1].height;
            let winHeight; // 窗口可使用高度
            wx.getSystemInfo({
                success: (res) => {
                    winHeight = res.windowHeight
                }
            })
            let scrollHeight = winHeight - headerHeight - footerHeight; // 滚动区域高度
            that.setData({
                scrollViewHeight: scrollHeight
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        if (options.data) {
            let params = JSON.parse(options.data)
            this.data.details = {
                ...params
            }
            console.log('进', params)

            this.setData({
                details: params,

            })
            this.computeScrollViewHeight();
            this.gethandle();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})